package com.rzico.sbl.ui.home

import androidx.lifecycle.MutableLiveData
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpMethod
import com.rzico.sbl.R
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.model.HomeContent
import com.rzico.sbl.model.ManagerContent
import com.rzico.sbl.model.MessageType
import com.rzico.sbl.model.RecordData
import com.rzico.sbl.model.ResponseModel
import com.rzico.sbl.model.ShareWorkerData
import com.rzico.sbl.other.getTodayDate
import com.rzico.sbl.viewmodel.CommonViewModel
import com.xinnuo.common.extend.optNotNull
import com.xinnuo.common.helper.intValue
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally
import org.json.JSONObject

class HomeViewModel : CommonViewModel() {

    val mNotice = MutableLiveData("")
    val mMessageEvent = MutableLiveData(0)
    val mOrderCount = MutableLiveData<Array<Int>>()
    val mDeliverCount = MutableLiveData<Array<Int>>()
    val mBarrelCount = MutableLiveData<Array<Int>>()

    // 管理员、店长
    fun masterIcons() = arrayListOf(
        ManagerContent("会员", R.mipmap.icon_home08),
        ManagerContent("商品", R.mipmap.icon_home10),
        ManagerContent("报单", R.mipmap.icon_home07),
        ManagerContent("报货", R.mipmap.icon_home09),
        ManagerContent("押桶", R.mipmap.icon_home22),
        ManagerContent("退桶", R.mipmap.icon_home18),
        ManagerContent("自提点", R.mipmap.icon_home20),
        ManagerContent("库存", R.mipmap.icon_home23)
    )

    // 管理员、店长（无报货）
    fun masterIconsNoReport() = arrayListOf(
        ManagerContent("会员", R.mipmap.icon_home08),
        ManagerContent("商品", R.mipmap.icon_home10),
        ManagerContent("报单", R.mipmap.icon_home07),
        ManagerContent("送货", R.mipmap.icon_home17),
        ManagerContent("押桶", R.mipmap.icon_home22),
        ManagerContent("退桶", R.mipmap.icon_home18),
        ManagerContent("自提点", R.mipmap.icon_home20),
        ManagerContent("库存", R.mipmap.icon_home23)
    )

    // 客服
    fun serveIcons() = arrayListOf(
        ManagerContent("报单", R.mipmap.icon_home07),
        ManagerContent("员工", R.mipmap.icon_home11),
        ManagerContent("推广", R.mipmap.icon_home16),
        ManagerContent("退桶", R.mipmap.icon_home18)
    )

    // 送水工
    fun sendIcons() = arrayListOf(
        ManagerContent("送货", R.mipmap.icon_home17),
        ManagerContent("员工", R.mipmap.icon_home11),
        ManagerContent("推广", R.mipmap.icon_home16),
        ManagerContent("退桶", R.mipmap.icon_home18)
    )

    // 推广员
    fun shareIcons() = arrayListOf(
        ManagerContent("员工", R.mipmap.icon_home11),
        ManagerContent("推广", R.mipmap.icon_home16)
    )

    // 自提店主
    fun pickMasterIcons() = arrayListOf(
        ManagerContent("自提点", R.mipmap.icon_home20),
        ManagerContent("订单", R.mipmap.icon_home21),
        ManagerContent("员工", R.mipmap.icon_home11),
        ManagerContent("库存", R.mipmap.icon_home23),
        ManagerContent("会员", R.mipmap.icon_home08),
        ManagerContent("商品", R.mipmap.icon_home10)
    )

    // 自提点
    fun pickIcons() = arrayListOf(
        ManagerContent("自提点", R.mipmap.icon_home20),
        ManagerContent("订单", R.mipmap.icon_home21)
    )

    /**
     * 获取公告信息
     */
    fun getNotice() {
        request(
            isLoadingBefore = false,
            isLoadingAfter = false,
            requestUrl = BaseUrl.notice,
            requestMethod = HttpMethod.GET,
            onSuccess = {
                mNotice.value = if (it == "{}") "" else it
            }
        )
    }

    /**
     * 消息数量
     */
    fun messageCount() =
        mDisposables.add(
            OkGo.get<BaseResponse<ResponseModel<MessageType>>>(BaseUrl.msgSum)
                .converter(object : JsonConvert<BaseResponse<ResponseModel<MessageType>>>() {})
                .adapt(FlowableBody())
                .compose(applyFlowableSchedulers())
                .map { it.data.data }
                .subscribeByFinally(
                    event = this,
                    onSuccess = { list ->
                        var totalCount = 0
                        list.forEach { totalCount += it.num.intValue }
                        mMessageEvent.value = totalCount
                    }
                )
        )

    /**
     * 获取订单和运单数量
     */
    fun getListCount(requestUrl: String) {
        request(
            isLoadingBefore = false,
            isLoadingAfter = false,
            requestUrl = requestUrl,
            onSuccess = {
                val obj = JSONObject(it)
                when (requestUrl) {
                    BaseUrl.orderCount -> {
                        mOrderCount.value = arrayOf(
                            obj.optNotNull("unpaid").intValue,
                            obj.optNotNull("unshipped").intValue,
                            obj.optNotNull("shipped").intValue,
                            obj.optNotNull("refund").intValue
                        )
                    }

                    BaseUrl.deliverCount -> {
                        mDeliverCount.value = arrayOf(
                            obj.optNotNull("unconfirmed").intValue,
                            (obj.optNotNull("undispatch").intValue
                                    + obj.optNotNull("confirmed").intValue),
                            obj.optNotNull("completed").intValue,
                            obj.optNotNull("hope").intValue
                        )
                    }
                }
            }
        )
    }

    /**
     * 本月销量列表
     */
    fun saleList() =
        OkGo.get<BaseResponse<ArrayList<HomeContent>>>(BaseUrl.homeSale)
            .converter(object : JacksonConvert<BaseResponse<ArrayList<HomeContent>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }

    /**
     * 推广员列表
     */
    fun shareList() =
        OkGo.get<BaseResponse<ResponseModel<ShareWorkerData>>>(BaseUrl.stsShareSum)
            .params(
                getParams(
                    "beginDate" to getTodayDate(),
                    "endDate" to getTodayDate(),
                    "pageStart" to 0,
                    "pageSize" to 200
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<ShareWorkerData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }

    /**
     * 接单列表
     */
    fun callList(
        onSuccess: (ResponseModel<RecordData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ResponseModel<RecordData>>>(BaseUrl.manuCallLis)
            .params(
                getParams(
                    "beginDate" to getTodayDate(),
                    "endDate" to getTodayDate(),
                    "pageStart" to 0,
                    "pageSize" to 200
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<RecordData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 获取退桶单数量
     */
    fun getBarrelCount() {
        request(
            isLoadingBefore = false,
            isLoadingAfter = false,
            requestUrl = BaseUrl.mngBarrelCut,
            onSuccess = {
                val obj = JSONObject(it)

                mBarrelCount.value = arrayOf(
                    obj.optNotNull("unconfirmed").intValue,
                    obj.optNotNull("dispatch").intValue,
                    obj.optNotNull("delivery").intValue,
                    obj.optNotNull("completed").intValue
                )
            }
        )
    }

}